package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps2d.model.LatLng;
import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.HoveringScrollview;
import com.stx.xhb.mylibrary.XBanner;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.OpenExternalMapAppUtils;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.activitity.minemodule.MyFaBuActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.javaBean.FloorInfoDetailBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

public class FloorInfoDetailActivity extends BaseActivity implements View.OnClickListener, ContributorView, XBanner.XBannerAdapter {

    private Context context;
    private ContributorPresenter contributorPresenter;
    private XBanner fragment_lyxq_xBanner;//轮播
    private TextView fragment_lyxq_title;//标题
    private TextView fragment_lyxq_bangong_land;//办公土地
    private TextView fragment_lyxq_shangyong_land;//商用土地
    private TextView fragment_lyxq_chuzu_land;//出租土地
    private TextView fragment_lyxq_name;
    private TextView fragment_lyxq_phone;//联系人电话
    private TextView fragment_lyxq_address;//地址
    private TextView fragment_lyxq_content;//简介信息
    private ImageView fragment_lyxq_back;//返回按钮
    private LinearLayout fragment_lyxq_boda_phone;//拨打联系电话
    private String bid;
    private String className;
    private final String BUILDINGDETAILFLAG = "BUILDINGDETAILFLAG";
    private final String BUILDINGDETAILRELEASEFLAG = "BUILDINGDETAILRELEASEFLAG";
    private FloorInfoDetailBean floorInfoDetailBean;
    private TextView tv_delete;
    private RelativeLayout layout_solve;
    private TextView tv_solve;
    private final String DELETEHOUSEDELETEFLAG="DELETEHOUSEDELETEFLAG";
    private MyToast myToast;
    private LinearLayout ll_mine_nav;
    private LinearLayout ll_mine_nav_replace;
    private RelativeLayout layout_nav_back;
    private HoveringScrollview refreshscrollView;
    private int layout_headTop;
    private LinearLayout ll_item;
    private TextView tv_headtitle;
    private LinearLayout layout_address;
    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.floorinfodetail_layout);
        context = this;
        myApplication=(MyApplication) getApplication();
        myToast=new MyToast(context);
        initView();
        Intent intent=getIntent();
        bid = intent.getStringExtra("bid");
        className=intent.getStringExtra("classname");
        contributorPresenter = new ContributorPresenter(this, context);
        if(className.equals(FloorInfoActivity.class.getSimpleName())||className.equals(FloorSearchActivity.class.getSimpleName()))
        {
            fragment_lyxq_boda_phone.setVisibility(View.VISIBLE);
            contributorPresenter.getBuildingDetail(bid, BUILDINGDETAILFLAG);
        }else if(className.equals(MyFaBuActivity.class.getSimpleName()))
        {
            tv_delete.setVisibility(View.VISIBLE);
            layout_solve.setVisibility(View.VISIBLE);
            contributorPresenter.getBuildingReleaseDetail(bid,BUILDINGDETAILRELEASEFLAG);
        }

        refreshscrollView.setOnScrollListener(new HoveringScrollview.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                layout_headTop = ll_mine_nav_replace.getTop() + context.getResources().getDimensionPixelOffset(R.dimen.fivedp);;
                int commentTop = ll_item.getTop()/2;
                if (scrollY > layout_headTop) {
                    if (layout_nav_back.getParent() != ll_mine_nav) {
                        ll_mine_nav_replace.removeView(layout_nav_back);
                        ll_mine_nav.addView(layout_nav_back);
                        tv_headtitle.setText("楼宇信息详情");
                    }
                } else {

                    if (layout_nav_back.getParent() != ll_mine_nav_replace) {
                        ll_mine_nav.removeView(layout_nav_back);
                        ll_mine_nav_replace.addView(layout_nav_back);
                        tv_headtitle.setText("楼宇信息详情");
                    }
                }

                int alph = (int) (Tools.div(255, commentTop) * scrollY);
                if (alph <= 255) {
                    tv_headtitle.setTextColor(Color.argb(alph, 255, 255, 255));
                    fragment_lyxq_back.setAlpha(alph);
                    layout_nav_back.setAlpha((float) Tools.div((Tools.div(255, commentTop) * scrollY),255));
                } else {
                    tv_headtitle.setTextColor(Color.argb(255, 255, 255, 255));
                    fragment_lyxq_back.setAlpha(255);
                    layout_nav_back.setAlpha(1);
                }
            }
        });

    }

    private void initView() {
        fragment_lyxq_title = (TextView) findViewById(R.id.fragment_lyxq_title);
        fragment_lyxq_phone = (TextView) findViewById(R.id.fragment_lyxq_phone);
        fragment_lyxq_content = (TextView) findViewById(R.id.fragment_lyxq_content);
        fragment_lyxq_address = (TextView) findViewById(R.id.fragment_lyxq_address);
        fragment_lyxq_bangong_land = (TextView) findViewById(R.id.fragment_lyxq_bangong_land);
        fragment_lyxq_shangyong_land = (TextView) findViewById(R.id.fragment_lyxq_shangyong_land);
        fragment_lyxq_chuzu_land = (TextView) findViewById(R.id.fragment_lyxq_chuzu_land);
        fragment_lyxq_boda_phone = (LinearLayout) findViewById(R.id.fragment_lyxq_boda_phone);
        fragment_lyxq_boda_phone.setOnClickListener(this);
        fragment_lyxq_back = (ImageView) findViewById(R.id.fragment_lyxq_back);
        fragment_lyxq_back.setOnClickListener(this);
        fragment_lyxq_xBanner = (XBanner) findViewById(R.id.fragment_lyxq_xBanner);
        fragment_lyxq_name=(TextView) findViewById(R.id.fragment_lyxq_name);
        tv_delete=(TextView) findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(this);
        layout_solve=(RelativeLayout) findViewById(R.id.layout_solve);
        tv_solve=(TextView) findViewById(R.id.tv_solve);

        ll_mine_nav = (LinearLayout)findViewById(R.id.ll_mine_nav);
        ll_mine_nav_replace = (LinearLayout)findViewById(R.id.ll_mine_nav_replace);
        layout_nav_back = (RelativeLayout) findViewById(R.id.layout_nav_back);
        layout_nav_back.setAlpha(0);
        refreshscrollView = (HoveringScrollview) findViewById(R.id.mineScrollView);
        ll_item = (LinearLayout) findViewById(R.id.ll_item);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        layout_address=(LinearLayout) findViewById(R.id.layout_address);
        layout_address.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_lyxq_boda_phone:
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + fragment_lyxq_phone.getText());
                intent.setData(data);
                startActivity(intent);
                break;
            case R.id.fragment_lyxq_back:
                finish();
                break;
            case R.id.tv_delete:
                contributorPresenter.getBuildingReleaseDelete(bid,DELETEHOUSEDELETEFLAG);
                break;
            case R.id.layout_address:
                try {
                        LatLng starLng=new LatLng(myApplication.getLatitude(),myApplication.getLongitude());
                        OpenExternalMapAppUtils.openMapDirection(this, starLng.longitude+"", starLng.latitude+"",
                                myApplication.getLocationAddress(),
                                "", "", fragment_lyxq_address.getText().toString().trim(), "智慧中国");
                }catch (Exception e)
                {

                }
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(DELETEHOUSEDELETEFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = (String) object;
                    if(jsonMessage.equals("41"))
                    {
                        myToast.showShort("删除成功");
                        Intent intent=getIntent();
                        setResult(1,intent);
                        finish();
                    }
                }else
                {
                    myToast.showShort("删除失败");
                }
            }else if (flag.equals(BUILDINGDETAILFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    floorInfoDetailBean = JsonTools.jsonStringToObject(jsonMessage, FloorInfoDetailBean.class);
                    if (floorInfoDetailBean != null) {
                        fragment_lyxq_title.setText(floorInfoDetailBean.getBuildingName());
                        fragment_lyxq_phone.setText(floorInfoDetailBean.getPhone());
                        if(floorInfoDetailBean.getIntro()==null || floorInfoDetailBean.getIntro().isEmpty())
                        {
                            fragment_lyxq_content.setText("暂无");
                        }else {
                            fragment_lyxq_content.setText(floorInfoDetailBean.getIntro());
                        }

                        fragment_lyxq_address.setText(floorInfoDetailBean.getAddr());
                        if(floorInfoDetailBean.getOfficeArea()>0)
                        {
                            fragment_lyxq_bangong_land.setText(floorInfoDetailBean.getOfficeArea() + "㎡");
                        }else {
                            fragment_lyxq_bangong_land.setText("暂无");
                        }

                        if(floorInfoDetailBean.getCommercialArea()>0)
                        {
                            fragment_lyxq_shangyong_land.setText(floorInfoDetailBean.getCommercialArea() + "㎡");
                        }else {
                            fragment_lyxq_shangyong_land.setText("暂无");
                        }

                        if(floorInfoDetailBean.getSellArea()>0)
                        {
                            fragment_lyxq_chuzu_land.setText(floorInfoDetailBean.getSellArea() + "㎡");
                        }else {
                            fragment_lyxq_chuzu_land.setText("暂无");
                        }

                        fragment_lyxq_xBanner.setData(floorInfoDetailBean.getBanners(), null);
                        fragment_lyxq_xBanner.setPoinstPosition(XBanner.CENTER);//指示器居于中
                        fragment_lyxq_xBanner.setmAdapter(this);
                        fragment_lyxq_name.setText(floorInfoDetailBean.getName());
                    }

                }
            }else if(flag.equals(BUILDINGDETAILRELEASEFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    floorInfoDetailBean = JsonTools.jsonStringToObject(jsonMessage, FloorInfoDetailBean.class);
                    if (floorInfoDetailBean != null) {
                        fragment_lyxq_title.setText(floorInfoDetailBean.getTitle());
                        fragment_lyxq_phone.setText(floorInfoDetailBean.getPhone());
                        if(floorInfoDetailBean.getIntro()==null || floorInfoDetailBean.getIntro().isEmpty())
                        {
                            fragment_lyxq_content.setText("暂无");
                        }else {
                            fragment_lyxq_content.setText(floorInfoDetailBean.getIntro());
                        }

                        fragment_lyxq_address.setText(floorInfoDetailBean.getAddr());

                        if(floorInfoDetailBean.getOfficeArea()>0)
                        {
                            fragment_lyxq_bangong_land.setText(floorInfoDetailBean.getOfficeArea() + "㎡");
                        }else {
                            fragment_lyxq_bangong_land.setText("暂无");
                        }

                        if(floorInfoDetailBean.getCommercialArea()>0)
                        {
                            fragment_lyxq_shangyong_land.setText(floorInfoDetailBean.getCommercialArea() + "㎡");
                        }else {
                            fragment_lyxq_shangyong_land.setText("暂无");
                        }

                        if(floorInfoDetailBean.getSellArea()>0)
                        {
                            fragment_lyxq_chuzu_land.setText(floorInfoDetailBean.getSellArea() + "㎡");
                        }else {
                            fragment_lyxq_chuzu_land.setText("暂无");
                        }

                        fragment_lyxq_xBanner.setData(floorInfoDetailBean.getBanners(), null);
                        fragment_lyxq_xBanner.setPoinstPosition(XBanner.CENTER);//指示器居于中
                        fragment_lyxq_xBanner.setmAdapter(this);
                        fragment_lyxq_name.setText(floorInfoDetailBean.getName());

                        if (floorInfoDetailBean.getState() == 0) {
                            tv_solve.setText("审核中");
                            tv_solve.setTextColor(Color.parseColor("#29D7F2"));
                        } else if (floorInfoDetailBean.getState() == 1) {
                            tv_solve.setText("发布成功");
                            tv_solve.setTextColor(Color.parseColor("#15CD1F"));
                        } else if (floorInfoDetailBean.getState() == 2) {
                            tv_solve.setText("被拒绝");
                            tv_solve.setTextColor(Color.parseColor("#FC5812"));
                        }
                    }

                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void loadBanner(com.stx.xhb.mylibrary.XBanner banner, Object model, View view, int position) {
        Glide.with(context).load(floorInfoDetailBean.getBanners().get(position)).into((ImageView) view);
    }

}
